package com.oracle.cmp.controller;

import com.oracle.cmp.common.Result;
import com.oracle.cmp.service.LoginService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/login.do")
public class LoginController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得请求中的用户名和密码
        String loginname=req.getParameter("loginname");
        String loginpwd=req.getParameter("loginpwd");
        String rembnerme=req.getParameter("rember");
//        String identity=req.getParameter("identity");
        //调用LoginService的login方法，获得Result对象
        LoginService loginService=new LoginService();
//        if (identity.equals("1")){
            Result result=loginService.login(loginname,loginpwd);
            //如果Result独享的succ是true 跳转到主页
            if (result.isSucc()){
                //将username、password、rememberMe的数据封装成Cookie发送给客户端
                Cookie usercoki=new Cookie("loginname",loginname);
                usercoki.setMaxAge(60);
                Cookie pwdcoki=new Cookie("loginpwd",loginpwd);
                pwdcoki.setMaxAge(60);
                Cookie rembcoki=new Cookie("rember",rembnerme);
                rembcoki.setMaxAge(60);
//                Cookie radiocoki=new Cookie("identy",identity);
//                radiocoki.setMaxAge(60);
                resp.addCookie(usercoki);
                resp.addCookie(pwdcoki);
                resp.addCookie(rembcoki);
//                resp.addCookie(radiocoki);
                //创建session防止非法登录
                HttpSession session=req.getSession();
                session.setAttribute("user",result.getData());

                //防止多点登录
                if (this.getServletContext().getAttribute(loginname)!=null){
                    HttpSession session1= (HttpSession) this.getServletContext().getAttribute(loginname);
                    if (!session1.getId().equals(session.getId())){
                        session1.invalidate();
                    }
                }
                this.getServletContext().setAttribute(loginname,session);
                req.getRequestDispatcher("/index.jsp").forward(req,resp);
            }else {
                //如果Result对象的succ是false 转到登录页
                req.setAttribute("result",result);
                req.getRequestDispatcher("/login.jsp").forward(req,resp);
            }
//        }else if (identity.equals("2")){
//            Result result=loginService.loginAdmin(username,password);
//            //如果Result独享的succ是true 跳转到主页
//            if (result.isSucc()){
//                //将username、password、rememberMe的数据封装成Cookie发送给客户端
//                Cookie usercoki=new Cookie("username",username);
//                usercoki.setMaxAge(60);
//                Cookie pwdcoki=new Cookie("pwd",password);
//                pwdcoki.setMaxAge(60);
//                Cookie rembcoki=new Cookie("rember",rembnerme);
//                rembcoki.setMaxAge(60);
//                Cookie radiocoki=new Cookie("identy",identity);
//                radiocoki.setMaxAge(60);
//                resp.addCookie(usercoki);
//                resp.addCookie(pwdcoki);
//                resp.addCookie(rembcoki);
//                resp.addCookie(radiocoki);
//                //创建session防止非法登录
//                HttpSession session=req.getSession();
//                session.setAttribute("user",result.getData());
//
//                //防止多点登录
//                if (this.getServletContext().getAttribute(username)!=null){
//                    HttpSession session1= (HttpSession) this.getServletContext().getAttribute(username);
//                    if (!session1.getId().equals(session.getId())){
//                        session1.invalidate();
//                    }
//                }
//                this.getServletContext().setAttribute(username,session);
//                req.getRequestDispatcher("WEB-INF/view/index.jsp").forward(req,resp);
//            }else {
//                //如果Result对象的succ是false 转到登录页
//                req.setAttribute("result",result);
//                req.getRequestDispatcher("WEB-INF/view/login.jsp").forward(req,resp);
//            }
        }


    }

